"use strict";

$(function () {
    //获取页面参数中的手机号码；
    function GetQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return r[2];return null;
    }
    var isPass = false;
    jQuery.acSiteUrl = 'https://ac.ppdai.com';
    var ajaxSync = function ajaxSync(url, data, callback, type, method) {
        if (jQuery.isFunction(data)) {
            type = type || callback;
            callback = data;
            data = undefined;
        }
        $.ajax({
            async: false,
            type: method || 'GET',
            url: url,
            data: data,
            success: callback,
            dataType: 'json'
        });
    };
    var regpasswd = /^(?![^a-zA-Z]+$)(?!\D+$).{6,16}$/,
        regpassword2 = /^[^<>]{6,16}$/;

    function handleClass(ele, addClassName, removeClassName, content) {
        $('#errMsg').text(content);
        ele.parent().siblings('.common-icon').removeClass(removeClassName).addClass(addClassName);
    }
    //图片验证码
    $('#ImgValidateCode').blur(function () {
        checkImgValidateCode();
    });

    function checkImgValidateCode() {
        var This = $("#ImgValidateCode");
        if ($.trim(This.val()).length < 1) {
            handleClass(This, 'wrong_hint', 'true_hint', '*图片验证码不能为空');
            isPass = false;
        } else {
            ajaxSync(jQuery.acSiteUrl + "/registercheck?callback=?", { name: "imgvalidatecode", value: This.val() }, function (data) {
                if (data.Code != 1) {
                    handleClass(This, 'wrong_hint', 'true_hint', data.Message);
                    isPass = false;
                } else {
                    handleClass(This, 'true_hint', 'wrong_hint', '');
                    isPass = true;
                }
            });
        }
        return isPass;
    }
    //手机验证码
    $('#SmsValidateCode').blur(function () {
        checkSmsValidateCode();
    });

    function checkSmsValidateCode() {

        var This = $("#SmsValidateCode");
        if ($.trim(This.val()).length < 1) {
            handleClass(This, 'wrong_hint', 'true_hint', '*手机验证码不能为空');
            isPass = false;
        } else {
            ajaxSync(jQuery.acSiteUrl + "/registercheck?callback=?", { name: "mobilevalidatecode", value: This.val() + "," + phoneNum }, function (data) {
                if (data.Code != 1) {
                    handleClass(This, 'wrong_hint', 'true_hint', data.Message);
                    isPass = false;
                } else {
                    handleClass(This, 'true_hint', 'wrong_hint', '');
                    isPass = true;
                }
            });
        }
        return isPass;
    }
    //密码
    $('#Password').on('blur', function () {
        checkPassword();
    });
    function checkPassword() {
        var This = $("#Password");
        if ($.trim($('#Password').val()).length < 1) {
            handleClass(This, 'wrong_hint', 'true_hint', '*密码不能为空');
            isPass = false;
        } else if (!regpasswd.test($.trim($('#Password').val()))) {
            handleClass(This, 'wrong_hint', 'true_hint', '*密码为6-16位字母和数字,且不能包含<>');
            isPass = false;
        } else if (!regpassword2.test($.trim($('#Password').val()))) {
            handleClass(This, 'wrong_hint', 'true_hint', '*密码为6-16位字母和数字,且不能包含<>');
            isPass = false;
        } else {
            handleClass(This, 'true_hint', 'wrong_hint', '');
            isPass = true;
        }
        return isPass;
    }

    //获得手机验证码
    $('#btnSendSms').on('click', function () {
        phoneNum = GetQueryString("phoneNum");
        if (checkImgValidateCode()) {
            acUtils.remainTime($(this), 30, null, '秒再发送', 'reg_mobile_remainTime', { expires: 1 / 24 / 60, path: '/', domain: '.ac.ppdai.com', secure: false });
            smsSend(phoneNum, $('#ImgValidateCode').val());
        }
    });
    if (jQuery.cookie('reg_mobile_remainTime') && jQuery.cookie('reg_mobile_remainTime').length > 0) {
        acUtils.remainTime('#btnSendSms', jQuery.cookie('reg_mobile_remainTime'), null, '秒再发送', 'reg_mobile_remainTime', { expires: 1 / 24 / 60, path: '/', domain: '.ac.ppdai.com', secure: false });
    }
    var smsSend = function smsSend(mobile, imgcode) {
        var sendUrl = jQuery.acSiteUrl + '/ValidateCode/MobileWithImgCode?mobilePhone=' + mobile + '&imgCode=' + imgcode + '&callback=?';
        $.getJSON(sendUrl, {}, function (res) {
            if (!res.Code == 1) {
                alert(res.Message);
            }
        });
    };
    //下载处理；
    var downLoad = function downLoad() {
        top.location.href = "https://a.app.qq.com/o/simple.jsp?pkgname=com.ppdai.huanapp";
    };
    //提交按钮
    $('#btnSubmit').click(function () {
        if (!checkPassword() || !checkSmsValidateCode()) {
            return false;
        }
        var $this = $(this);
        var redirect = $("#Redirect").val();
        if (redirect == undefined) {
            redirect = "";
        }
        var weixinCode = $("#WeixinCode").val();
        if (weixinCode == undefined) {
            weixinCode = "";
        }
        var weixinFailRedirect = $("#WeixinFailRedirect").val();
        if (weixinFailRedirect == undefined) {
            weixinFailRedirect = "";
        }
        var data = {
            ActivityID: $('#ActivityID').val(),
            Mobile: phoneNum, //此处从页面url中获取手机号码；
            Password: $('#Password').val(),
            SmsValidateCode: $('#SmsValidateCode').val(),
            UserRole: $("#UserRole").val(),
            Redirect: redirect,
            WeixinCode: weixinCode,
            WeixinFailRedirect: weixinFailRedirect
        };
        $.post(jQuery.acSiteUrl + '/ActivityPage/Index', data, function (res) {
            if (res.Code == 1) {
                $.ajax({
                    type: 'get',
                    dataType: 'json',
                    url: "https://m.ppdai.com/Activity/BindUserIdWithMobileRelation",
                    data: { mobile: phoneNum },
                    complete: function complete() {
                        downLoad();
                    },
                    success: function success(data) {
                        if (data.Result == 0) {
                            console.log(data.ResultMessage);
                        }
                    },
                    error: function error(XMLHttpRequest, textStatus, errorThrown) {
                        console.log(data.ResultMessage);
                    }
                });
            } else {
                $('#errMsg').text(res.Message);
            }
        });
    });
});